//
//  ZBLiveStatisticsItem.m
//  ZhiBo
//
//  Created by huangtie on 2017/11/6.
//  Copyright © 2017年 huangtie. All rights reserved.
//

#import "ZBLiveStatisticsItem.h"

@interface  ZBLiveStatisticsItem()

@property (nonatomic , copy) NSString *iconName;

@property (nonatomic , copy) NSString *numberText;

@end

@implementation ZBLiveStatisticsItem

- (instancetype)initIconName:(NSString *)iconName
{
    self = [ZBLiveStatisticsItem buttonWithType:UIButtonTypeCustom];
    if (self) {
        self.iconName = iconName;
        self.numberText = @"0";
        [self setImage:[UIImage imageNamed:iconName] forState:UIControlStateNormal];
        
        self.size = CGSizeMake(80, 55);
        [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.titleLabel.font = FONT_NORMAL_14;
    }
    return self;
}

- (void)layoutSelf:(NSInteger)textNumber
{
    if (textNumber > 10000) {
        CGFloat number = (CGFloat)textNumber / 10000.0;
        self.numberText = [NSString stringWithFormat:@"%.1f万",number];
    }else{
        self.numberText = INT_TO_STR(textNumber);
    }
    
    [self setTitle:self.numberText forState:UIControlStateNormal];
    
    // -> 先将button的image和title居左上，方便计算insets
    self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    self.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
    // <-
    
    // 只有在(button.width >= image.width + title.width)时才能计算出正确的imageView.width和titleLabel.width
    // -> 先将button.width扩大两倍，便于正确的计算
    CGRect buttonFrame = self.frame;
    buttonFrame.size.width = buttonFrame.size.width * 2;
    self.frame = buttonFrame;
    // <-
    CGFloat margin = 6.0;
    CGFloat spacing = 4.0;
    CGSize imageSize = self.imageView.frame.size;
    CGSize titileSize = self.titleLabel.frame.size;
    // -> 将放大两倍的的button.width恢复正常
    buttonFrame = self.frame;
    buttonFrame.size.width = buttonFrame.size.width / 2;
    self.frame = buttonFrame;
    // <-
    CGSize buttonSize = self.frame.size;
    self.imageEdgeInsets = UIEdgeInsetsMake(margin, (buttonSize.width - imageSize.width) / 2, .0, .0);
    self.titleEdgeInsets = UIEdgeInsetsMake(imageSize.height + margin + spacing, (buttonSize.width - titileSize.width) / 2 - imageSize.width, .0, .0);
}








@end
